﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using NHibernate;
using SocialLearning;

/// <summary>
/// Summary description for Chat_User
/// </summary>
public partial class Chat_User_Control
{
    public AMP_User getUser(int? UserID)
    {
        // Open the session
        ISession session = NHibernateHelper.GetCurrentSession();

        // Load the User from the database
        AMP_User User = (AMP_User)session.Load(typeof(AMP_User), UserID);

        return User;
    }

    public List<AMP_User> getUsers()
    {
        List<AMP_User> Users = new List<AMP_User>();

        // Open the session
        ISession session = NHibernateHelper.GetCurrentSession();

        // Load the Users from the database
        session.CreateCriteria(typeof(AMP_User)).List(Users);

        return Users;
    }

    public void saveOrUpdateUser(AMP_User User)
    {
        ISession session = NHibernateHelper.GetCurrentSession();
        ITransaction transaction = null;

        try
        {
            // Start transaction
            transaction = session.BeginTransaction();

            // Save or update (saves if the id is null, otherwise it updates)
            session.SaveOrUpdate(User);

            // Commit transaction
            transaction.Commit();
        }
        catch (Exception ex)
        {
            transaction.Rollback();
            throw ex;
        }
    }

    public void deleteUser(AMP_User User)
    {
        ISession session = NHibernateHelper.GetCurrentSession();
        ITransaction transaction = null;

        try
        {
            // Start transaction
            transaction = session.BeginTransaction();

            // Delete
            session.Delete(User);

            // Commit transaction
            transaction.Commit();
        }
        catch (Exception ex)
        {
            transaction.Rollback();
            throw ex;
        }
    }
}
